<script setup lang="ts">
import { reactive, ref, withDefaults } from 'vue'
import type { GoodsItem } from '@/types/global'
import { onReachBottom, onReady } from '@dcloudio/uni-app'
import { getHomeGoodsGuessLikeAPI } from '@/services/home'

const props = withDefaults(defineProps<{
  busType?: string,
  pagination: {
    pages: number
    pageNo: number,
    pageSize: number
  },
  apiFunc?: (params: { page: number, pageSize: number }) => Promise<any>
}>(),{
  apiFunc: getHomeGoodsGuessLikeAPI
});

const more = ref();
const loadmoreText = ref('加载更多');
const emits = defineEmits(["queryMore"]);
const STATUS = {IDLE: 'loadmore',  LOADING: 'loading',  NO_MORE: 'nomore'};

let list: GoodsItem[] = reactive([]);
let status = ref(STATUS.IDLE);
/***
 *计算页数
 *page
 */
const calculatePages = (total: number, pageSize: number) => {
  if (total === 0 || pageSize === 0) {
    return 0
  }
  return Math.ceil(total / pageSize)
}
const initFuc = async () => {
  if (status.value === STATUS.LOADING) return;
  // 将状态设置为'loading'
  status.value = STATUS.LOADING;
  await props.apiFunc({ page: props.pagination.pageNo, pageSize: props.pagination.pageSize }).then(res=>{
    const pages = calculatePages(res.result.counts, props.pagination.pageSize);
    list.push(...res.result.items);
    emits("queryMore", pages);
  }).catch(e=>{
    status.value = STATUS.IDLE;
    loadmoreText.value = "重新加载"
  });
  status.value = STATUS.IDLE;
}

const query = async () => {
  // 如果页码大于总页数，设置状态为'nomore'并返回
  if (props.pagination.pages > 0 && props.pagination.pageNo > props.pagination.pages) {
    status.value = STATUS.NO_MORE;
    return;
  }
  await initFuc();
}
onReady(async () => {
  await query()
})
onReachBottom(async () => {  await query()  })

</script>
<template>
  <view class="guess">
    <!--    列表类型-->
    <template v-if="busType=='list'">
      <navigator
        class="guess-item guess-item1"
        v-for="item in list"
        :key="item.id"
        :url="`/pages/goods/goods?id=${item.id}`"
      >
        <view> {{ item.name }} </view>
      </navigator>
    </template>
    <!--    其它类型-->
    <template v-else>
      <navigator
        class="guess-item"
        v-for="item in list"
        :key="item.id"
        :url="`/pages/goods/goods?id=${item.id}`"
      >
        <image class="image" mode="aspectFill" :src="item.picture"></image>
        <view class="name"> {{ item.name }} </view>
        <view class="price">
          <text class="small">¥</text>
          <text>{{ item.price }}</text>
        </view>
      </navigator>
    </template>
    <up-loadmore ref="more" :loadmore-text="loadmoreText" :status="status" icon-color="#028BF7" marginTop="20" />
  </view>
</template>

<style lang="scss">
:host {
  display: block;
}
/* 猜你喜欢 */
.guess {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;
  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .guess-item1{
    width: 100%;
  }
  .image {
    width: 304rpx;
    height: 304rpx;
  }
  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }
  .small {
    font-size: 80%;
  }
}
</style>